package com.health.db.service.sm.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONException;
import com.google.gson.Gson;
import com.health.db.model.WXUserInfo;
import com.health.db.service.sm.IWXService;
import com.health.mapp.bean.WXTokenModel;
import com.health.pay.tencent.common.Configure;

public class WXServiceImpl implements IWXService {

	private static Logger logger = Logger.getLogger(WXServiceImpl.class);
	@Override
	public WXTokenModel getAccessToken(String code) {
		// TODO Auto-generated method stub

		StringBuffer param=new StringBuffer();
		param.append("appid=");
		param.append(Configure.getAppLoginID());
		param.append("&");
		param.append("secret=");
		param.append(Configure.getAppLoginSecret());
		param.append("&");
		param.append("code=");
		param.append(code);
		param.append("&");
		param.append("grant_type=authorization_code");
		
		String url = "https://api.weixin.qq.com/sns/oauth2/access_token?"+param.toString();
		URI uri = URI.create(url);
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(uri);

		HttpResponse response;
		try {
			response = client.execute(get);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();

				BufferedReader reader = new BufferedReader(
						new InputStreamReader(entity.getContent(), "UTF-8"));
				StringBuilder sb = new StringBuilder();

				for (String temp = reader.readLine(); temp != null; temp = reader
						.readLine()) {
					sb.append(temp);
				}

				logger.debug("微信获取Token接口返回数据：" + sb.toString().trim());
				Gson gson = new Gson();
				WXTokenModel wxTokenModel = gson.fromJson(sb.toString().trim(),
						WXTokenModel.class);
				return wxTokenModel;
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public WXTokenModel refreshToken(String refreshToken) {
		// TODO Auto-generated method stub
		StringBuffer param=new StringBuffer();
		param.append("appid=");
		param.append(Configure.getAppLoginID());
		param.append("&");
		param.append("refresh_token=");
		param.append(refreshToken);
		param.append("&");
		param.append("grant_type=refresh_token");
		
		String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?"+param.toString();
		URI uri = URI.create(url);
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(uri);

		HttpResponse response;
		try {
			response = client.execute(get);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();

				BufferedReader reader = new BufferedReader(
						new InputStreamReader(entity.getContent(), "UTF-8"));
				StringBuilder sb = new StringBuilder();

				for (String temp = reader.readLine(); temp != null; temp = reader
						.readLine()) {
					sb.append(temp);
				}

				logger.debug("微信获取Token接口返回数据：" + sb.toString().trim());
				Gson gson = new Gson();
				WXTokenModel wxTokenModel = gson.fromJson(sb.toString().trim(),
						WXTokenModel.class);
				return wxTokenModel;
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	public WXUserInfo getWXUserInfo(String access_token,String openid)
	{
		// TODO Auto-generated method stub
		StringBuffer param=new StringBuffer();
		param.append("access_token=");
		param.append(access_token);
		param.append("&");
		param.append("openid=");
		param.append(openid);

		String url = "https://api.weixin.qq.com/sns/userinfo?"+param.toString();
		URI uri = URI.create(url);
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(uri);

		HttpResponse response;
		try {
			response = client.execute(get);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();

				BufferedReader reader = new BufferedReader(
						new InputStreamReader(entity.getContent(), "UTF-8"));
				StringBuilder sb = new StringBuilder();

				for (String temp = reader.readLine(); temp != null; temp = reader
						.readLine()) {
					sb.append(temp);
				}

				logger.debug("微信获取Token接口返回数据：" + sb.toString().trim());
				Gson gson = new Gson();
				WXUserInfo wxTokenModel = gson.fromJson(sb.toString().trim(),
						WXUserInfo.class);
				return wxTokenModel;
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public WXTokenModel getDocAccessToken(String code) {
		StringBuffer param=new StringBuffer();
		param.append("appid=");
		param.append(Configure.getAppDocLoginID());
		param.append("&");
		param.append("secret=");
		param.append(Configure.getAppDocLoginSecret());
		param.append("&");
		param.append("code=");
		param.append(code);
		param.append("&");
		param.append("grant_type=authorization_code");

		String url = "https://api.weixin.qq.com/sns/oauth2/access_token?"+param.toString();
		URI uri = URI.create(url);
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(uri);

		HttpResponse response;
		try {
			response = client.execute(get);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();

				BufferedReader reader = new BufferedReader(
						new InputStreamReader(entity.getContent(), "UTF-8"));
				StringBuilder sb = new StringBuilder();

				for (String temp = reader.readLine(); temp != null; temp = reader
						.readLine()) {
					sb.append(temp);
				}

				logger.debug("微信获取Token接口返回数据：" + sb.toString().trim());
				Gson gson = new Gson();
				WXTokenModel wxTokenModel = gson.fromJson(sb.toString().trim(),
						WXTokenModel.class);
				return wxTokenModel;
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public WXTokenModel refreshDocToken(String refreshToken) {
		StringBuffer param=new StringBuffer();
		param.append("appid=");
		param.append(Configure.getAppDocLoginID());
		param.append("&");
		param.append("refresh_token=");
		param.append(refreshToken);
		param.append("&");
		param.append("grant_type=refresh_token");

		String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?"+param.toString();
		URI uri = URI.create(url);
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(uri);

		HttpResponse response;
		try {
			response = client.execute(get);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();

				BufferedReader reader = new BufferedReader(
						new InputStreamReader(entity.getContent(), "UTF-8"));
				StringBuilder sb = new StringBuilder();

				for (String temp = reader.readLine(); temp != null; temp = reader
						.readLine()) {
					sb.append(temp);
				}

				logger.debug("微信获取Token接口返回数据：" + sb.toString().trim());
				Gson gson = new Gson();
				WXTokenModel wxTokenModel = gson.fromJson(sb.toString().trim(),
						WXTokenModel.class);
				return wxTokenModel;
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public WXUserInfo getDocWXUserInfo(String access_token, String openid) {
		// TODO Auto-generated method stub
		StringBuffer param=new StringBuffer();
		param.append("access_token=");
		param.append(access_token);
		param.append("&");
		param.append("openid=");
		param.append(openid);

		String url = "https://api.weixin.qq.com/sns/userinfo?"+param.toString();
		URI uri = URI.create(url);
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(uri);

		HttpResponse response;
		try {
			response = client.execute(get);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();

				BufferedReader reader = new BufferedReader(
						new InputStreamReader(entity.getContent(), "UTF-8"));
				StringBuilder sb = new StringBuilder();

				for (String temp = reader.readLine(); temp != null; temp = reader
						.readLine()) {
					sb.append(temp);
				}

				logger.debug("微信获取Token接口返回数据：" + sb.toString().trim());
				Gson gson = new Gson();
				WXUserInfo wxTokenModel = gson.fromJson(sb.toString().trim(),
						WXUserInfo.class);
				return wxTokenModel;
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}
}
